<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_ticket
 *
 * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.view');

/**
 * View class for a list of ticket.
 *
 * @package     Joomla.Administrator
 * @subpackage  com_ticket
 * @since       1.6
 */
class CeltawebticketsViewTickets extends JView
{
	protected $categories;
	protected $items;
	protected $pagination;
	protected $state;

	/**
	 * Method to display the view.
	 *
	 * @param   string  $tpl  A template file to load. [optional]
	 *
	 * @return  mixed  A string if successful, otherwise a JError object.
	 *
	 * @since   1.6
	 */
	public function display($tpl = null)
	{
		// Initialise variables.
		$this->categories	= $this->get('CategoryOrders');
		$this->items		= $this->get('Items');
		$this->pagination	= $this->get('Pagination');
		$this->state		= $this->get('State');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}

		$this->addToolbar();
		require_once JPATH_COMPONENT . '/models/fields/ticketuser.php';
		// Include the component HTML helpers.
		JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');

		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	protected function addToolbar()
	{
		require_once JPATH_COMPONENT . '/helpers/celtawebtickets.php';

		$canDo = CeltawebticketsHelper::getActions($this->state->get('filter.category_id'));
		$user = JFactory::getUser();
		JToolBarHelper::title(JText::_('COM_CELTAWEBTICKETS_MANAGER_TICKETS'), 'celtawebtickets-tickets');
                
                JToolBarHelper::back( 'COM_CELTAWEBTICKETS_HEADING_CPANEL', 'index.php?option=com_celtawebtickets' );
                JToolBarHelper::divider(); 
                
                if ($canDo->get('core.create') || (count($user->getAuthorisedCategories('com_celtawebtickets', 'core.create'))) > 0) {
			JToolBarHelper::addNew('ticket.add');
		}

		if (($canDo->get('core.edit')) || ($canDo->get('core.edit.own'))) {
			JToolBarHelper::editList('ticket.edit');
		}

		if ($canDo->get('core.edit.state'))
		{
			if ($this->state->get('filter.state') != 2)
			{
				JToolBarHelper::divider();
				JToolBarHelper::publish('ticket.publish', 'JTOOLBAR_PUBLISH', true);
				JToolBarHelper::unpublish('ticket.unpublish', 'JTOOLBAR_UNPUBLISH', true);
                                JToolBarHelper::checkin('tickets.checkin');
                                JToolBarHelper::divider();
			}

		}

		if ($this->state->get('filter.state') == -2 && $canDo->get('core.delete')) {
			JToolBarHelper::deleteList('', 'tickets.delete', 'JTOOLBAR_EMPTY_TRASH');
			JToolBarHelper::divider();
		} elseif ($canDo->get('core.edit.state')) {
			JToolBarHelper::trash('tickets.trash');
			JToolBarHelper::divider();
		}

		if ($canDo->get('core.admin'))
		{
			JToolBarHelper::preferences('com_celtawebtickets');
			JToolBarHelper::divider();
		}
		JToolBarHelper::help('JHELP_COMPONENTS_CELTAWEBTICKETS_CELTAWEBTICKETS');
	}
}
